﻿using MCPlatform.MotionLib.Base.Environment;
using Panuon.WPF.UI;


namespace MCPlatform.MotionLib.Base.AssitantClass
{
    public class SaveConfigData
    {

        public static void SaveConfig<T>(string path, T model) where T : class,INameProvider
        {
            List<T>? ModelInfolList = ConvertJson.ReadFileToList<T>(path);
            if (ModelInfolList != null)
            {

                foreach (var item in ModelInfolList)
                {
                    if (item.ControlName.Equals(model.ControlName))
                    {
                        ModelInfolList[ModelInfolList.IndexOf(item)] = model;
                        ConvertJson.WriteJsonToFile(path, ModelInfolList);
                        Toast.Show("应用成功，已保存", targetWindow: ToastWindow.MainWindow);
                        return;

                    }

                }


                ModelInfolList.Add(model);
                ConvertJson.WriteJsonToFile(path, ModelInfolList);
                Toast.Show("未找到此点位号配置，已创建并写入成功！", targetWindow: ToastWindow.MainWindow);
            }
            else
            {

                ModelInfolList = new List<T>
                {
                   model
                };
                ConvertJson.WriteJsonToFile(path, ModelInfolList);
                Toast.Show("未找到配置文件，已创建并写入成功！", targetWindow: ToastWindow.MainWindow);
            }
        }


        public static void SaveConfigNotWindow<T>(string path, T model) where T : class, INameProvider
        {
            List<T>? ModelInfolList = ConvertJson.ReadFileToList<T>(path);
            if (ModelInfolList != null)
            {

                foreach (var item in ModelInfolList)
                {
                    if (item.ControlName.Equals(model.ControlName))
                    {
                        ModelInfolList[ModelInfolList.IndexOf(item)] = model;
                        ConvertJson.WriteJsonToFile(path, ModelInfolList);                       
                        Toast.Show("应用成功，已保存", targetWindow: ToastWindow.MainWindow);
                        return;

                    }

                }


                ModelInfolList.Add(model);
                ConvertJson.WriteJsonToFile(path, ModelInfolList);
                Toast.Show("未找到此点位号配置，已创建并写入成功！", targetWindow: ToastWindow.MainWindow);
            }
            else
            {

                ModelInfolList = new List<T>
                {
                   model
                };
                ConvertJson.WriteJsonToFile(path, ModelInfolList);
                Toast.Show("未找到配置文件，已创建并写入成功！", targetWindow: ToastWindow.MainWindow);
            }
        }


        public static void SaveConfig<T>(string path, T[] model) where T : class,INameProvider
        {
            ConvertJson.WriteJsonToFile(path, model);
            Toast.Show("应用成功，已保存", targetWindow: ToastWindow.MainWindow);
        }


     


    }
}

    

